﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.Interfejsi;
using MySql.Data.MySqlClient;
using OOAD_PROJEKAT;

namespace DAL
{
    partial class DAL
    {
        public class AutomobilDAO : IDaoCrud<Automobil>
        {

            protected MySqlCommand c;

            public long create(Automobil a)
            {
                try
                {

                    c = new MySqlCommand();
                    c.CommandText = "insert into automobili values (@idAutomobila, @registarskeOznake, @godinaProizvodnje, @snagaKw, @prosaoTehnicki, @tip, @proizvodjac, @izbrisan, @idKlijenta)";
                    c.Parameters.AddWithValue("@idAutomobila", "");
                    c.Parameters.AddWithValue("@registarskeOznake", a.RegistarskeOznake);
                    c.Parameters.AddWithValue("@godinaProizvodnje", a.GodinaProizvodnje);
                    c.Parameters.AddWithValue("@snagaKw", a.SnagaKw);
                    c.Parameters.AddWithValue("@prosaoTehnicki", a.ProsaoTehnicki);
                    c.Parameters.AddWithValue("@tip", a.Tip);
                    c.Parameters.AddWithValue("@proizvodjac", a.Proizvodjac);
                    c.Parameters.AddWithValue("@izbrisan", 0);
                    c.Parameters.AddWithValue("@idKlijenta", a.IdKlijenta);


                    c.Connection = con;
                    c.ExecuteNonQuery();

                    return c.LastInsertedId;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<Automobil> getAll()
            {
                c = new MySqlCommand("select * from automobili where izbrisan=0", con);
                MySqlDataReader r = c.ExecuteReader();
                try
                {
                    List<Automobil> automobili = new List<Automobil>();
                    while (r.Read())
                        automobili.Add(new Automobil(r.GetInt32("idAutomobila"), r.GetString("registarskeOznake"), r.GetDateTime("godinaProizvodnje"), r.GetDouble("snagaKw"),
                                   r.GetBoolean("prosaoTehnicki"), r.GetString("tip"), r.GetString("proizvodjac"), r.GetInt32("idKlijenta")));
                    return automobili;
                }
                catch (Exception e)
                {
                    throw e;
                }
                finally
                {
                    r.Close();
                }
            }


            public Automobil read(Automobil a)
            {
                try
                {
                    string commandStr = "select * from automobili where idAutomobila=" + a.Id + " and izbrisan = 0";
                    c = new MySqlCommand(commandStr, con);
                    MySqlDataReader r = c.ExecuteReader();
                    Automobil pr = null;
                    if (r.Read())
                        pr = new Automobil(r.GetInt32("idAutomobila"), r.GetString("registarskeOznake"), r.GetDateTime("godinaProizvodnje"), r.GetDouble("snagaKw"),
                                   r.GetBoolean("prosaoTehnicki"), r.GetString("tip"), r.GetString("proizvodjac"), r.GetInt32("idKlijenta"));
                    r.Close();
                    return pr;
                }

                catch (Exception e)
                {
                    throw e;
                }
            }

            public Automobil update(Automobil a)
            {
                try
                {
                    String commandStr = "update automobili set registarskeOznake = ?a, ";
                    commandStr += "godinaProizvodnje = ?b, snagaKw = ?c, prosaoTehnicki = ?d, ";
                    commandStr += "tip = ?e, proizvodjac = ?f, idKlijenta = ?g ";
                    commandStr += "where idAutomobila=" + a.Id + " and izbrisan=0";

                    c = new MySqlCommand(commandStr, con);
                    c.Parameters.AddWithValue("?a", a.RegistarskeOznake);
                    c.Parameters.AddWithValue("?b", a.GodinaProizvodnje);
                    c.Parameters.AddWithValue("?c", a.SnagaKw);
                    c.Parameters.AddWithValue("?d", a.ProsaoTehnicki);
                    c.Parameters.AddWithValue("?e", a.Tip);
                    c.Parameters.AddWithValue("?f", a.Proizvodjac);
                    c.Parameters.AddWithValue("?g", a.IdKlijenta);

                    c.ExecuteNonQuery();
                    return read(a);
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public void delete(Automobil a)//brisanje postavlja kolonu "izbrisan" na 1
            {
                try
                {
                    string commandStr = "update automobili set izbrisan=1 where idAutomobila=" + a.Id;
                    c = new MySqlCommand(commandStr, con);
                    c.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    throw e;
                }

            }

            public Automobil getById(int id)
            {
                try
                {
                    Automobil a = new Automobil(id, "", DateTime.Now, 0, true, "", "", 0);
                    return read(a);
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<Automobil> getByExample(string name, string value)
            {
                try
                {
                    c = new MySqlCommand("select * from automobili where izbrisan=0 and " + name + "= '" + value + "'", con);
                    MySqlDataReader r = c.ExecuteReader();
                    List<Automobil> automobili = new List<Automobil>();
                    while (r.Read())
                        automobili.Add(new Automobil(r.GetInt32("idAutomobila"), r.GetString("registarskeOznake"), r.GetDateTime("godinaProizvodnje"), r.GetDouble("snagaKw"),
                                   r.GetBoolean("prosaoTehnicki"), r.GetString("tip"), r.GetString("proizvodjac"), r.GetInt32("idKlijenta")));

                    r.Close();
                    return automobili;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

        }
    }
}